package com.region.integral.common_user.consume.dao.impl;

import org.springframework.stereotype.Component;

import com.region.integral.common_user.consume.dao.IKCurrencyBuyGoods;
import com.region.integral.common_user.obtain.vo.IntegralData;
import com.region.integral.common_user.pojo.KCurrencyEntity;
import com.region.integral.exception.IntegralException;
import com.region.integral.hibernate.BaseHibernate;
import com.region.integral.util.CommonUserLogUtil;

@Component("iBuyGoods")
public class KCurrencyBuyGoodsImpl extends BaseHibernate implements
		IKCurrencyBuyGoods {
	@Override
	public void buyGoods(int userid, int kNum,IntegralData integralData) throws IntegralException {
		KCurrencyEntity kcEntity = getKCurrencyEntity(userid);
		if (kcEntity != null) {
			int currk = kcEntity.getKcurrency();
			int maxk = kcEntity.getMaxKCurrency();
			if(currk >= kNum){
				int totalk = currk - kNum;
				kcEntity.setKcurrency(totalk);
				if(maxk<totalk){
					kcEntity.setMaxKCurrency(totalk);
				}
				integralData.setSuccee(true);
				hibernateTemplate.update(kcEntity);
				hibernateTemplate.save(CommonUserLogUtil.log(userid, kNum, "byGoods_k"));
				
			}
			else{
				integralData.setSuccee(false);
			}
		}
	}

}
